package com.example.routeplansystem.controller;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.routeplansystem.anotation.UserPermissionConf;
import com.example.routeplansystem.constant.ResponseStatus;
import com.example.routeplansystem.entity.dto.ResponseEntity;
import com.example.routeplansystem.entity.po.Question;
import com.example.routeplansystem.entity.vo.QuestionsVO;
import com.example.routeplansystem.exception.ServiceException;
import com.example.routeplansystem.service.QuestionService;
import com.example.routeplansystem.utils.DateUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("question")
@Api(tags = "方案模块")
@Slf4j
@UserPermissionConf
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    //创建问题:
    @PostMapping("/question")
    @ApiOperation("创建问题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionName", value = "问题名称", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    })
    public ResponseEntity<Integer> insertQuestion(@RequestBody Question question, BindingResult result) {

        log.info("------创建问题(Controller) 问题信息：" + question + "------" + DateUtil.getFormatDate());

        //参数校验：
        if (result.hasErrors()) {
            String message = result.getAllErrors().get(0).getDefaultMessage();
            return new ResponseEntity<>(ResponseStatus.FAILURE_RESPONSE, message, null);
        }

        return questionService.insertQuestion(question);
    }

    //查询当前用户创建的所有问题：
    @Validated
    @PostMapping("/getQuestions")
    @ApiOperation("获取该用户所有路径规划问题信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", dataType = "int", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", dataType = "int", value = "当前页容量", required = true)
    })
    public ResponseEntity<List<Question>> getQuestions(@RequestBody QuestionsVO questionsVO,@RequestAttribute("userId") Integer userId) {
        if (questionsVO.getUserId() == null) {
            throw new ServiceException(HttpStatus.NOT_MODIFIED.value() + "", "用户未登录");
        }
        log.info("------查询当前用户创建的问题列表：（创建信息）" + questionsVO + "------" + DateUtil.getFormatDate());
        int currentPage = questionsVO.getCurrentPage();
        int pageSize = questionsVO.getPageSize();
        if (currentPage == 0 || pageSize == 0) {
            currentPage = 1;
            pageSize = 5;
        }

        return questionService.getQuestions(userId, currentPage, pageSize);
    }

    //删除问题：
    @Validated
    @DeleteMapping("removeQuestion")
    @ApiOperation("删除问题")
    @ApiImplicitParam(name = "questionId", dataType = "int", value = "问题Id", paramType = "query")
    public ResponseEntity<String> removeQuestion(@NotNull(message = "questionId不能为空") @RequestParam("questionId") int questionId) {
        return questionService.removeQuestion(questionId);
    }
}
